package com.zl.controller.validate;

import com.zl.controller.annotation.CheckPassword;
import com.zl.model.User;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author ZLL
 * @date create in 2018/04/26
 */
public class CheckPasswordValidator implements ConstraintValidator<CheckPassword, User> {
    @Override
    public void initialize(CheckPassword checkPassword) {

    }

    @Override
    public boolean isValid(User user, ConstraintValidatorContext context) {
            if(user == null) {
                return true;
            }

            //没有填密码
            if(!StringUtils.hasText(user.getPassword())) {
                context.disableDefaultConstraintViolation();
                context.buildConstraintViolationWithTemplate("{password.null}")
                        .addPropertyNode("password")
                        .addConstraintViolation();
                return false;
            }

            if(!StringUtils.hasText(user.getConfirmation())) {
                context.disableDefaultConstraintViolation();
                context.buildConstraintViolationWithTemplate("{password.confirmation.null}")
                        .addPropertyNode("confirmation")
                        .addConstraintViolation();
                return false;
            }

            //两次密码不一样
            if (!user.getPassword().trim().equals(user.getConfirmation().trim())) {
                context.disableDefaultConstraintViolation();
                context.buildConstraintViolationWithTemplate("{password.confirmation.error}")
                        .addPropertyNode("confirmation")
                        .addConstraintViolation();
                return false;
            }

            return true;
        }


}
